package cn.cdiothree.service.impl;

import cn.cdiothree.dataobject.ProductCategory;
import cn.cdiothree.dao.ProductCategoryMapper;
import cn.cdiothree.service.ProductCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CumThing
 * @since 2020-02-02
 */
@Service
@Slf4j
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper,ProductCategory> implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    /**
     * 根据类目id查找类目
     * @param id
     * @return
     */
    @Override
    public ProductCategory findById(Integer id) {
        log.info("查找类目{}",id);
        ProductCategory productCategory= productCategoryMapper.selectById(id);
        return productCategory;
    }

    /**
     * 查找所有商品类目
     * @return
     */
    @Override
    public List<ProductCategory> findAll() {
        log.info("查找所有类目");
        QueryWrapper<ProductCategory> productCategoryQueryWrapper=new QueryWrapper<>();
        return productCategoryMapper.selectList(productCategoryQueryWrapper);
    }

    /**
     * 根据类目id列表查找类目表
     * @param parentIdList
     * @return
     */
    @Override
    public List<ProductCategory> findByParentIdIn(List<Integer> parentIdList) {
        log.info("查找类目列表");
        QueryWrapper<ProductCategory> productCategoryQueryWrapper=new QueryWrapper<>();
        List productCategoryList=new ArrayList<>();
        for (Integer integer:parentIdList){
            productCategoryQueryWrapper.eq("parent_id",integer);
            List<ProductCategory> productCategoryList1=productCategoryMapper.selectList(productCategoryQueryWrapper);
            productCategoryList.add(productCategoryList1);
        }
        return productCategoryList;
    }

    /**
     * 插入商品类目
     * @param productCategory
     * @return
     */
    @Override
    public int insert(ProductCategory productCategory) {
        log.info("插入商品");
        return productCategoryMapper.insert(productCategory);
    }
}
